package com.dfrz.controller;

import com.dfrz.common.ResultEnum;
import com.dfrz.entity.*;
import com.dfrz.service.VideoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author 29877
 */
@RestController
public class VideoController {
    @Resource
    private VideoService videoService;

    //普通用户发布视频
    @PostMapping("/upload")
    public Result uploadVideo(String videoTitle,String videoIntro,Integer userId,
                              MultipartFile video, MultipartFile videoImg) throws Exception {
        String date = LocalDate.now().toString();
        Video videoInfo = new Video(videoTitle,videoIntro,date,userId);
        Integer detailId = videoService.addVideoU(videoInfo,video,videoImg);
        if(detailId == null){
            return Result.error(ResultEnum.ERROR.getCode(), "发布失败");
        }else{
            return Result.success(ResultEnum.SUCCESS);
        }
    }

    //管理员发布视频
    @PostMapping("/uploads")
    public Result uploadVideo(String videoTitle, String videoIntro, String  videoTime,
                              String videoMold, String upTime, Integer userId, MultipartFile videoImg) throws Exception {
        Video video = new Video( videoTitle, videoIntro, videoTime, userId, videoMold, upTime);
        int i=videoService.addVideoM(video,videoImg);
        return Result.success(ResultEnum.SUCCESS);
    }

    //管理员模糊查询视频为视频添加续集
    @GetMapping("/getVideo/{videoTitle}")
    public Result selectVideoByName(@PathVariable String videoTitle){
        List<Video> videos = videoService.selectvideoByName(videoTitle);
        return Result.success(videos);
    }


    //分页查询video 传入页码和每页需要多少条
    @GetMapping("static/selectlimit")
    public Result selectLimit(Integer page,Integer num){
        List<Video> videos=videoService.selectLimit(page,num);
        return Result.success(videos);
    }

    @GetMapping("/selectvideobyid")
    public Result selectvideobyid(Integer vid){
        Video video=videoService.selectvideobyid(vid);
        return Result.success(video);
    }

    @GetMapping("static/selectorderbyscore")
    public Result selectorderbyscore(){
        List<Video> videos=videoService.selectorderbyscore();
        return Result.success(videos);
    }

    //预览推荐
    @GetMapping("static/recommend")
    public Result recommend(){
        List<Utilrecommend> recommends=videoService.seleltrecommend();
        return Result.success(recommends);
    }

    //猜你喜欢
    @GetMapping("static/guess")
    public Result guess(){
        List<Video> videos=videoService.selectrandom();
        return Result.success(videos);
    }

    //新热推荐
    @GetMapping("static/hotanime")
    public Result selecthot(){
        List<Video> videos=videoService.selecthot();
        return Result.success(videos);
    }

    //搜索时的模糊查询
    @GetMapping("static/searchvideo")
    public Result searchVideo(String word){
        List<Video> videos = videoService.searchVideo(word);
        return Result.success(videos);
    }

    //视频编辑获取所有的视频信息
    @GetMapping("/getVideoInfoByEdit")
    public Result getVideoInfoByEdit(){
        Map<String,Object> videos =videoService.getVideoInfoByEdit();
        return Result.success(videos);
    }

    //分页请求视频信息
    @GetMapping("/getPagingEdit")
    public Result getPaging(Integer page){
        List<Video> pages = videoService.getPageingEdit(page);
        return Result.success(pages);
    }

    @DeleteMapping("/deleteVideoEdit/{videoId}")
    public Result deleteVideoEdit(@PathVariable("videoId") Integer videoId){
        videoService.deleteVideoEdit(videoId);
        return Result.success(ResultEnum.SUCCESS);
    }

    //视频更新
    @PostMapping("/upVideoInfo")
    public Result updataCheck(Integer videoId, String videoTitle, String videoIntro, String  videoTime,
                              String videoMold, String upTime, Integer userId, MultipartFile videoImg) throws Exception {
        Video video = new Video( videoId,videoTitle, videoIntro, videoTime, userId, videoMold, upTime);
        videoService.upVideoInfo(videoImg,video);
        return Result.success(ResultEnum.SUCCESS);
    }
}
